package cn.lanqiao.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyVetoException;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.Font;
import java.awt.Insets;

public class UsersFrm extends JFrame {
	private JPanel contentPane;
	JDesktopPane desktopPane = null;
	private static final long serialVersionUID = -3749566882604678819L;
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					UsersFrm frame = new UsersFrm();
					frame.setVisible(true);
					
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public UsersFrm() {
		getContentPane().setBackground(Color.WHITE);
		setTitle("教务管理系统");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		

		JMenuBar menuBar = new JMenuBar();
		menuBar.setMargin(new Insets(5, 5, 0, 0));
		menuBar.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 20));
		setJMenuBar(menuBar);

		JMenu menu = new JMenu("账号管理");
		menu.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 20));
		menuBar.add(menu);
		
		JMenuItem menuItem_1 = new JMenuItem("账户管理");
		menuItem_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//打开账号管理界面
				ManUserFrm manuser =new ManUserFrm();
				manuser.setVisible(true);
				desktopPane.add(manuser);
				manuser.setLocation(200, 50);
			}
		});
		menuItem_1.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 20));
		menu.add(menuItem_1);

		JMenu ManScore = new JMenu("成绩管理");
		ManScore.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 20));
		menuBar.add(ManScore);

		JMenuItem Searchscore = new JMenuItem("成绩查询");
		Searchscore.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 20));
		Searchscore.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// 容器中有就激活，没有就创建
				String title = "学生成绩";
				// 1.找到容器内所以子窗体
				JInternalFrame[] childFrames = desktopPane.getAllFrames();
				// 2.遍历，查找
				boolean isExists = false;
				for (JInternalFrame f : childFrames) {
					if (f.getTitle().equals(title)) {
						// 3.设置当前窗体为激活窗体
						try {
							f.setSelected(true);
							isExists = true;
						} catch (PropertyVetoException e1) {
							e1.printStackTrace();
						}
						break;
					}
				}
				if (!isExists) {
					TeacherSources addChildFrm = new TeacherSources();// 弹出子窗体
					addChildFrm.setVisible(true);
					desktopPane.add(addChildFrm);
					addChildFrm.setLocation(170, 70);
				}
			}
		});
		ManScore.add(Searchscore);

		JMenu Manstu = new JMenu("学籍管理");
		Manstu.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 20));
		menuBar.add(Manstu);

		JMenuItem menuItem = new JMenuItem("学生信息管理");
		menuItem.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 20));
		menuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// 容器中有就激活，没有就创建
				String title = "学籍信息管理";
				// 1.找到容器内所以子窗体
				JInternalFrame[] childFrames = desktopPane.getAllFrames();
				// 2.遍历，查找
				boolean isExists = false;
				for (JInternalFrame f : childFrames) {
					if (f.getTitle().equals(title)) {
						// 3.设置当前窗体为激活窗体
						try {
							f.setSelected(true);
							isExists = true;
						} catch (PropertyVetoException e1) {
							e1.printStackTrace();
						}
						break;
					}
				}
				if (!isExists) {
					StudentInformation information = new StudentInformation();
					information.setVisible(true);
					desktopPane.add(information);
					information.setLocation(50, 70);
				}
			}
		});
		Manstu.add(menuItem);
		
		JMenu menu_1 = new JMenu("退出");
		menu_1.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 20));
		menuBar.add(menu_1);
		
		JMenuItem menuItem_2 = new JMenuItem("退出");
		menuItem_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		menuItem_2.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 20));
		menu_1.add(menuItem_2);
		setBounds(100, 90, 1000, 700);
		UsersFrm.this.setLocationRelativeTo(null);
		getContentPane().setLayout(null);

		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(0, 0, 0, 0));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);

		desktopPane = new JDesktopPane();
		desktopPane.setBounds(0, 0, 0, 0);
		desktopPane.setBackground(Color.WHITE);
		getContentPane().add(desktopPane);
	}
}
